package com.seckill.product.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seckill.product.entity.MiddleEntity;
import com.seckill.product.entity.ShopEntity;
import com.seckill.product.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.seckill.product.entity.ActivitySurfaceEntity;
import com.seckill.product.service.ActivitySurfaceService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author zhuxz
 * @email sunlightcs@gmail.com
 * @date 2022-06-19 09:38:15
 */
@RestController
@RequestMapping("product/activitysurface")
public class ActivitySurfaceController {
    @Autowired
    private ActivitySurfaceService ActivitySurfaceService;

    @Autowired
    private ShopService shopService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = ActivitySurfaceService.queryPage(params);

        return R.ok().put("page", page);
    }

    //计算毫秒值
    @RequestMapping("/haomiao")
    public R haomiao(@RequestParam Map<String, Object> params){
        String sid = (String) params.get("sid");//获取到要计算的id
        //根据id查询数据
        QueryWrapper<ActivitySurfaceEntity> w = new QueryWrapper<>();
        int sid1 = Integer.parseInt(sid);
        ActivitySurfaceEntity entity = ActivitySurfaceService.getById(sid1);
        //计算毫秒值
        Date createTime = entity.getCreateTime();
        Date endTime = entity.getEndTime();
        Date date = new Date();
        //如果开始时间小于系统时间
        if (createTime.before(date)){
            //判断多久之后开始秒杀
            if (endTime.after(date)){
                Long a= date.getTime()-createTime.getTime();
                a=a/1000; //计算秒
                //将数据传回前台
                return R.ok().put("miao",a);
            }
        }else {
            //如果开始时间大于系统时间
            //判断多久之后结束
            if (endTime.after(date)){
                Long a= endTime.getTime()-date.getTime();
                a=a/1000; //计算秒
                //将数据传回前台
                return R.ok().put("miao",a);
            }
        }

        return R.error();
    }


    //秒杀活动发布
    @RequestMapping("/release")
    public R release(@RequestBody ActivitySurfaceEntity surfaceEntity){
        ShopEntity shopEntity = new ShopEntity();
        if (surfaceEntity.getSid()!=null){
             shopEntity= shopService.getById(surfaceEntity.getSid());
        }
        surfaceEntity.setSeckillName(shopEntity.getSname());

        ActivitySurfaceService.save(surfaceEntity);

        return R.ok();
    }




    /**
     * 信息
     */
    @RequestMapping("/info/{seckillId}")
    public R info(@PathVariable("seckillId") Integer seckillId){
		ActivitySurfaceEntity ActivitySurface = ActivitySurfaceService.getById(seckillId);

        return R.ok().put("ActivitySurface", ActivitySurface);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ActivitySurfaceEntity ActivitySurface){
		ActivitySurfaceService.save(ActivitySurface);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ActivitySurfaceEntity ActivitySurface){
		ActivitySurfaceService.updateById(ActivitySurface);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] seckillIds){
		ActivitySurfaceService.removeByIds(Arrays.asList(seckillIds));

        return R.ok();
    }

}
